<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Commission;
use App\Admin\Models\Users;
use App\Admin\Models\Goods;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use function foo\func;

use Chenyulingxi\LaravelAdmin\GridExporter\Exporter;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeExport;
class CommissionController extends AdminController
{
    protected $title = '返佣管理';

    protected function grid()
    {
        $grid = new Grid(new Commission);
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('user', function ($query) {
                        $query->where('realname', "{$this->input}");
                    });
                }, '收益用户');
            });
            $filter->column(1/3, function ($filter) {
            $filter->where(function ($query){
                $query->whereHas('users',function ($query){
                    $query->where('realname',"{$this->input}");
                });
            },'购买用户');
            });
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query){
                    $query->whereHas('goods',function ($query){
                        $query->where('name','like',"%{$this->input}%");
                    });
                },'商品名称');
            });
        });

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $page = request('page');
            if($page){
                $page = ($page-1)*10;
            }
            $row->column('number', $page +$number+1);
        });
        $grid->column('user_id','收益用户')->display(function ($user_id){
            return  Users::where(array('id'=>$user_id))->value('realname');
        });
        $grid->column('money','返佣金额');
        $grid->column('goods_id','购买商品')->display(function ($goods_id){
            return  Goods::where(array('id'=>$goods_id))->value('name');
        });
        $grid->column('buy_user_id','购买用户昵称')->display(function ($buy_user_id){
            return Users::where(array('id'=>$buy_user_id))->value('realname');
        });
        $grid->column('mobile','购买用户电话')->display(function (){
           $uid = $this->buy_user_id;
           return DB::table('users')->whereId($uid)->value('mobile');
        });
        $grid->column('created_at','返佣时间');
        $grid->column('money', '收益统计')->sortable()->totalRow(function ($money) {
            return "<span class='text-danger text-bold'><i class='fa fa-yen'></i> {$money} 元</span>";
        });
        return $grid;
    }
}
